﻿using HolterDatabase.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HolterDatabase.ViewModel
{
    public class DataBaseSetViewModel : ViewModel
    {
        public DataBaseSetViewModel(DatabaseSet databaseSet)
        {
            this.databaseSet = databaseSet;
            this.databaseTable = databaseSet.GetTable();

            databaseSet.DataTable.RowChanged += OnChange;
            databaseSet.DataTable.RowDeleted += OnChange;
        }

        void OnChange(object sender, DataRowChangeEventArgs e)
        {
            IsDirty = true;
        }

        public void RefreshTable()
        {
            databaseSet.RefreshTable();
            IsDirty = false;
        }

        public bool UpdateTable()
        {
            var result = databaseSet.UpdateTable();
            IsDirty = false;
            return result;
        }

        public DataView DataView
        {
            get
            {
                return databaseTable.DefaultView;
            }
        }

        public void Export()
        {
            databaseSet.Export();
        }

        public void Import()
        {
            databaseSet.Import();
        }

        private DatabaseSet databaseSet;
        private DataTable databaseTable;
    }
}
